﻿function round2Fixed(value) {
    value = +value;

    if (isNaN(value))
        return NaN;

    // Shift
    value = value.toString().split('e');
    value = Math.round(+(value[0] + 'e' + (value[1] ? (+value[1] + 2) : 2)));

    // Shift back
    value = value.toString().split('e');
    return (+(value[0] + 'e' + (value[1] ? (+value[1] - 2) : -2))).toFixed(2);
}
function round(value, exp) {
    if (typeof exp === 'undefined' || +exp === 0)
        return Math.round(value);

    value = +value;
    exp = +exp;

    if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0))
        return NaN;

    // Shift
    value = value.toString().split('e');
    value = Math.round(+(value[0] + 'e' + (value[1] ? (+value[1] + exp) : exp)));

    // Shift back
    value = value.toString().split('e');
    return +(value[0] + 'e' + (value[1] ? (+value[1] - exp) : -exp));
}
function generatePassword() {
    var length = 8,
        charset = "abcdefghijklnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
        retVal = "";
    for (var i = 0, n = charset.length; i < length; ++i) {
        retVal += charset.charAt(Math.floor(Math.random() * n));
    }
    return retVal;
}
function resetUpload() {
    $(".k-upload-files").remove();
    $(".k-upload-status").remove();
    $(".k-upload.k-header").addClass("k-upload-empty");
    $(".k-upload-button").removeClass("k-state-focused");
    $(".k-upload-files.k-reset").find("li").remove();
}
function format2(n, currency) {
    return currency + " " + n.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1,");
}
function setValueCheckBox(object) {
    var listCheckBox = $("#aspnetForm").find($('input[type="checkbox"]'));
    for (var i = 0; i < listCheckBox.length; i++) {
        var name = listCheckBox[i].name;
        var value = object[name];
        $("#" + name).prop("checked", value);
    }
}
function setValueTextBox(object) {
    var listTextBox = $("#aspnetForm").find($('input[type="text"]'));
    for (var i = 0; i < listTextBox.length; i++) {
        var name = listTextBox[i].name;
        var value = object[name];
        $("[name=" + name + "]").attr("value", value);
    }
}

function getValueCheckBox(object) {
    var listCheckBox = $("#aspnetForm").find($('input[type="checkbox"]'));
    for (var i = 0; i < listCheckBox.length; i++) {
        var name = listCheckBox[i].name;
        object[name] = $("#" + name).prop('checked');
    }
}
function getValueTextBox(object) {
    var listTextBox = $("#aspnetForm").find($('input[type="text"]'));
    for (var i = 0; i < listTextBox.length; i++) {
        var name = listTextBox[i].name;
        object[name] = $("#" + name).val();
    }
}
function getUrlParameter(name) {
    return decodeURIComponent((location.search.match(RegExp("[?|&]" + name + '=(.+?)(&|$)')) || [, null])[1]);
}
var onSelectUploadDataFile = function (event) {
    $.each(event.files, function (index, value) {
        if (value.extension !== '.csv') {
            event.preventDefault();
            alert("Please upload csv files");
        }
        console.log("Name: " + value.name);
        console.log("Size: " + value.size + " bytes");
        console.log("Extension: " + value.extension);
    });
};
function ajaxindicatorstart(text) {
    
    if (jQuery('body').find('#resultLoading').attr('id') != 'resultLoading') {
        jQuery('body').append('<div id="resultLoading" style="display:none"><div><img src="images/process.gif"><div>' + text + '</div></div><div class="bg"></div></div>');
    }

    jQuery('#resultLoading').css({
        'width': '100%',
        'height': '100%',
        'position': 'fixed',
        'z-index': '10000000',
        'top': '0',
        'left': '0',
        'right': '0',
        'bottom': '0',
        'margin': 'auto'
    });

    jQuery('#resultLoading .bg').css({
       
        'opacity': '0.7',
        'width': '100%',
        'height': '100%',
        'position': 'absolute',
        'top': '0'
    });

    jQuery('#resultLoading>div:first').css({
        'width': '250px',
        'height': '75px',
        'text-align': 'center',
        'position': 'fixed',
        'top': '0',
        'left': '0',
        'right': '0',
        'bottom': '0',
        'margin': 'auto',
        'font-size': '16px',
        'z-index': '10',
        'color': '#000000'

    });

    jQuery('#resultLoading .bg').height('100%');
    jQuery('#resultLoading').fadeIn(300);
    jQuery('body').css('cursor', 'wait');
}

function ajaxindicatorstop() {
    jQuery('#resultLoading .bg').height('100%');
    jQuery('#resultLoading').fadeOut(300);
    jQuery('body').css('cursor', 'default');
}

jQuery(document).ajaxStart(function () {
    //show ajax indicator
    ajaxindicatorstart('');
}).ajaxStop(function () {
    //hide ajax indicator
    ajaxindicatorstop();
});

  function setMasterMessage (msg, msgType) {
        var msgdecoded = decodeURIComponent((msg + '').replace(/\+/g, '%20'));
        notify(msgdecoded, msgType, 5000);
    }

    function notify(message, type, timeout) {
        // default values
        message = typeof message !== 'undefined' ? message : 'Hello!';
        type = typeof type !== 'undefined' ? type : 'error';
        if (type.length == 0) {
            type = 'error';
        }
        timeout = typeof timeout !== 'undefined' ? timeout : 1000;

        if ($('#notification').length < 1) {
            markup = '<div id="notification" class="information"><span>Hello!</span><a class="close" href="#">x</a></div>';
            $('body').append(markup);
        }

        // elements
        $notification = $('#notification');
        $notificationSpan = $('#notification span');
        $notificationClose = $('#notification a.close');

        // set the message
        $notificationSpan.text(message);

        // setup click event
        $notificationClose.click(function (e) {
            e.preventDefault();
            $notification.css('top', '-500px');
        });

        // for ie6, scroll to the top first
        //        if ($.browser.msie && $.browser.version < 7) {
        //            $('html').scrollTop(0);
        //        }

        // hide old notification, then show the new notification
        $notification.css('top', '-50px').stop().removeClass().addClass(type).animate({
            top: 0
        }, 300, function () {
            $notification.delay(timeout).animate({
                top: '-500px'
            }, 300);
        });
    }